<html>
<head>
  <title>06开发坏境搭建</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="657"/>
<h1>06开发坏境搭建</h1>

<div>
<span><div><div><div><span style="font-size: 32px;"><span style="font-size: 32px; color: rgb(28, 51, 135); font-weight: bold;">virtualenv</span></span></div><div><br/></div><div>在开发Python应用程序的时候，系统安装的Python3只有一个版本：3.4。所有第三方的包都会被pip安装到Python3的site-packages目录下。</div><ul><li>首先，我们用pip安装virtualenv：</li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">$ pip3 install virtualenv</span></div></div><div><br/></div><div>然后，假定我们要开发一个新的项目，需要一套独立的Python运行环境，可以这么做：</div><ul><li>第一步，创建目录：</li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Mac:~ michael$ mkdir myproject</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Mac:~ michael$ cd myproject/</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Mac:myproject michael$</span></div></div><div><br/></div><ul><li>第二步，创建一个独立的Python运行环境，命名为venv：</li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Mac:myproject michael$ virtualenv --no-site-packages venv</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Using base prefix '/usr/local/.../Python.framework/Versions/3.4'</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">New python executable in venv/bin/python3.4</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Also creating executable in venv/bin/python</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Installing setuptools, pip, wheel...done.</span></div></div><div><br/></div><div>命令virtualenv就可以创建一个独立的Python运行环境，<span style="color: rgb(129, 182, 255);">我们还加上了参数--no-site-packages，这样，已经安装到系统Python环境中的所有第三方包都不会复制过来</span>，这样，我们就得到了一个不带任何第三方包的“干净”的Python运行环境。</div><div>新建的Python环境被放到当前目录下的venv目录。</div><ul><li>有了venv这个Python环境，可以用source进入该环境：</li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Mac:myproject michael$ source venv/bin/activate</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">(venv)Mac:myproject michael$</span></div><div><br/></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">~~~~</span><font style="font-size: 16pt;"><span style="background-color: rgb(251, 250, 248); color: rgb(227, 0, 0); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 16pt; font-weight: bold;">~venv\Scripts\activate      #for windows</span></font></div></div><div><br/></div><div>注意到命令提示符变了，有个(venv)前缀，表示当前环境是一个名为venv的Python环境。</div><ul><li>下面正常安装各种第三方包，并运行python命令：</li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">(venv)Mac:myproject michael$ pip install jinja2</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">...</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Successfully installed jinja2-2.7.3 markupsafe-0.23</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">(venv)Mac:myproject michael$ python myapp.py</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">...</span></div></div><div><br/></div><div>在venv环境下，用pip安装的包都被安装到venv这个环境下，系统Python环境不受任何影响。也就是说，venv环境是专门针对myproject这个应用创建的。</div><ul><li>退出当前的venv环境，使用deactivate命令：</li></ul><div><br/></div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">(venv)Mac:myproject michael$ deactivate</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">Mac:myproject michael$</span></div></div><div><br/></div><div>此时就回到了正常的环境，现在pip或python均是在系统Python环境下执行。</div><div>完全可以针对每个应用创建独立的Python运行环境，这样就可以对每个应用的Python环境进行隔离。</div><div>virtualenv是如何创建“独立”的Python运行环境的呢？原理很简单，就是把系统Python复制一份到virtualenv的环境，用命令source venv/bin/activate进入一个virtualenv环境时，virtualenv会修改相关环境变量，让命令python和pip均指向当前的virtualenv环境。</div><div><br/></div><div><span style="font-size: 32px; font-weight: bold; color: rgb(28, 51, 135);">常用包的安装命令：</span></div><div>pip install virtualenv -i https://pypi.tuna.tsinghua.edu.cn/simple some-package</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">pypi.python.org 官方源</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">pypi.douban.com 豆瓣源，福州</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">pypi.hustunique.com 华中科技大学源，武汉</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">pypi.tuna.tsinghua.edu.cn 清华源，北京</span></div></div><div><br/></div><div>pip install  -i https://pypi.tuna.tsinghua.edu.cn/simple  numpy scipy </div><div><span style="font-size: 19px; color: rgb(28, 51, 135);">scipy安装</span></div><div>1、<a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml">http://www.lfd.uci.edu/~gohlke/pythonlibs/</a></div><div>     <a href="http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy">http://www.lfd.uci.edu/~gohlke/pythonlibs</a>/</div><div>2、Ctrl + f 定位响应.whl，下载</div><div>2、直接进入pip所在的目录\c:\python34\scripts ，然后，把你要安装的whl文件都复制在这里啦。 接下来使用pip install “文               </div><div>     件路径+whl文件名”, 如 pip install  E:\ANACONDA\Scripts\lxml-3.4.4-cp34-none-win_amd64.whl</div><div><br/></div><div><span style="font-size: 32px; color: rgb(28, 51, 135); font-weight: bold;">make安装：</span></div><div>mingw安装：</div><div>教程：<a href="http://blog.csdn.net/pdcxs007/article/details/8582559">http://blog.csdn.net/pdcxs007/article/details/8582559</a></div><ol><li>下载mingw：<a href="https://sourceforge.net/projects/mingw/?source=typ_redirect">https://sourceforge.net/projects/mingw/?source=typ_redirect</a></li><li><br/></li></ol><div><span style="font-size: 32px; color: rgb(28, 51, 135); font-weight: bold;">windows中搭建unix的anaconda开发环境</span></div><ol><li>&quot;xming&quot;安装</li><li>开启WSL bash，在windows中打开unix功能，打开开发者模式，从应用商店下载ubuntu</li><li>in WSL bash install the display stuff to work with xming</li></ol><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">sudo apt-get install x11-apps</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">export DISPLAY=localhost:0.0</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">nano ~/.bashrc  #(add  export DISPLAY=localhost:0.0  at the end. Ctrl+X to exit/save)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">sudo apt-get install gnome-calculator #will get you GTK</span></div></div><ol start="4"><li>  Now in WSL bash install Anaconda. downloading the .sh file (eg with curl -O &quot;[the http link to latest anaconda]&quot; and running it with bash [the file].sh.</li></ol><div>              Anaconda3 will now be installed into this location:</div><div>                                                                      /home/liwenjian/anaconda3</div><ol start="5"><li>  Now make an environment and activate it</li></ol><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">conda create -n gym python=3.5 anaconda</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">source activate gym</span></div></div><div>   6.   get the gym repo， install these gym dependencies mentioned on openai gym repo</div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">git clone https://github.com/openai/gym.git</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">cd gym</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">apt-get install -y python-numpy python-dev cmake zlib1g-dev libjpeg-dev xvfb libav-tools xorg-dev python-opengl libboost-all-dev libsdl2-dev swig</span></div></div><div><br/></div><div>   7.   Now install libgcc with conda</div><div>    conda install libgcc</div><div>       Now build gym</div><div>    pip install -e '.[all]'</div><div><span style="font-size: 32px; color: rgb(20, 113, 145); font-weight: bold;">设置conda国内默认镜像</span></div><div>conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/</div><div>conda config --set show_channel_urls yes</div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div style="text-align: center;"><font color="#1C3387" style="font-size: 24pt;"><span style="color: rgb(28, 51, 135); font-size: 24pt; font-weight: bold;">opencv配置</span></font></div><div style="text-align: center;"><div><br/></div></div><div><span style="line-height: 1.45; color: rgb(28, 51, 135); font-size: 18pt;">第一种方法</span></div><div><a href="http://blog.csdn.net/poem_qianmo/article/details/19809337">http://blog.csdn.net/poem_qianmo/article/details/19809337</a></div><div><br/></div><div><br/></div><div><span style="color: rgb(28, 51, 135); font-size: 18pt;">第二种方法</span></div></div><div><br/></div><table><colgroup><col></col><col></col></colgroup><tbody><tr><td><div><br/></div></td><td><div style="margin-top: 1em; margin-bottom: 1em;">The right way to install OpenCV 3.0.0 for Python 3.4.2 on Windows 8.1:</div><ol><li>Go to <a href="http://www.lfd.uci.edu/%7Egohlke/pythonlibs/#opencv">http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv</a></li><li>Download OpenCV, NumPy and SciPy</li></ol><div style="margin-top: 1em; margin-bottom: 1em;">For me I needed:</div><ul><li>opencv_python-3.0.0-cp34-none-win_amd64.whl</li><li>scipy‑0.16.0‑cp34‑none‑win_amd64.whl</li><li>numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl</li><ol start="3"><li>Download and install Visual C++ 2010 Express from <a href="http://microsoft-visual-cpp-express.soft32.com/free-download/">http://microsoft-visual-cpp-express.soft32.com/free-download/</a></li><li>Open a command prompt as administrator</li><li>Change directory like &quot;cd C:\Users\me\Downloads&quot; to where I downloaded the file</li><li>Copy and paste this: &quot;python -m pip install -U pip&quot;</li><li>Then install the libraries. Copy and paste this:<br/>
pip install opencv_python-3.0.0-cp34-none-win_amd64.whl<br/>
pip install scipy‑0.16.0‑cp34‑none‑win_amd64.whl<br/>
pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl</li></ol></ul><div style="margin-top: 1em; margin-bottom: 1em;">That is all.</div><div style="margin-top: 1em; margin-bottom: 1em;">-------------&gt; Tricky Python &lt;------------</div></td></tr></tbody></table><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div><div><br/></div></span>
</div></body></html> 